﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
using System.Threading.Tasks;
namespace BlackBox.Models
{
    public class ChatHub : Hub
    {
        static List<string> ConnectedUsers = new List<string>();

        #region Tasks 

            #region onConnected
        public override Task OnConnected()
        {
            if (!ConnectedUsers.Contains(Context.User.Identity.Name.ToString()))
            {
                ConnectedUsers.Add(Context.User.Identity.Name.ToString());
            }

            return Clients.All.connected(Context.User.Identity.Name, ConnectedUsers);
        }
        #endregion

            #region onDisconnected
        public override Task OnDisconnected()
        {
            ConnectedUsers.Remove(Context.User.Identity.Name.ToString());
            return Clients.All.disconnected(Context.User.Identity.Name);
        }
        #endregion

            #region List of Users
        public Task ListUsers()
        {
            return Clients.All.ListUsers(ConnectedUsers);
        }
        #endregion

        #endregion

        #region SendMessage
        //Sends a message to all
        public void SendMessage(string message)
        {
            string from = Context.User.Identity.Name;
            Clients.All.AddMessage(from, message);
        }
        #endregion

    }

}